/**
 * createTime: 2018/4/17 11:23
 * author: zhibin.zhao
 * description:
 */
const Koa = require('koa');
const app = new Koa();
const bodyParser = require('koa-bodyparser');
app.use(bodyParser());
app.use(async(ctx)=>{
    if(ctx.url === '/' && ctx.method === 'GET'){
        //显示表单页面
        let html = `
            <h1>Koa2 POST</h1>
            <form method="post" action="/">
                <p>userName</p>
                <input name="userName"/>
                <p>age</p>
                <input name="age"/>
                <p>website</p>
                <input name="website"/>
                <button type="submit">submit</button>
            </form>
        `;
        ctx.body = html;
    }else if(ctx.url === '/' && ctx.method === 'POST'){
        let postData = ctx.request.body;
        ctx.body = postData;
    }else{
        ctx.body = '<h1>404</h1>';
    }
});
app.listen(3000, ()=>{
    console.log("app is starting at port 3000");
})